package com.xuecheng.checkcode.service.impl;

import com.xuecheng.checkcode.service.CheckCodeService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author 勾新杰
 * @version 1.0
 * @description 使用redis存储验证码，测试用
 * @date 2022/9/29 18:36
 */
@Component("RedisCheckCodeStore")
@RequiredArgsConstructor
public class RedisCheckCodeStore implements CheckCodeService.CheckCodeStore {

    private final RedisTemplate<String, Object> redisTemplate;

    /**
     * 设置验证码
     * @param key 验证码对应的key
     * @param value 验证码
     * @param expire 过期时间，单位分钟
     */
    @Override
    public void set(String key, String value, Integer expire) {
        redisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);
    }

    /**
     * 获取验证码
     * @param key 验证码对应的key
     * @return 验证码
     */
    @Override
    public String get(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }

    /**
     * 删除验证码
     * @param key 验证码对应的key
     */
    @Override
    public void remove(String key) {
        redisTemplate.delete(key);
    }
}
